home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SPX30.ZIP / SPX_DOC.ZIP / SPX_FNC.DOC < prev    next >
Encoding:
Text File  |  1994-06-13  |  10.8 KB  |  387 lines

  1. { SPX Library Version 3.0  Copyright 1993 Scott D. Ramsay }
  2.  
  3.  SPX_FNC  contains general functions and procedures that are used
  4. by most of the units.
  5.  
  6. ───────────────────────────────────────────────────────────────────────────
  7. function sgn(h:integer):integer;
  8.  
  9.   Returns -1 if H is less than zero
  10.            1 if H is greater than zero
  11.            0 if H is equal to zero
  12. ───────────────────────────────────────────────────────────────────────────
  13. function max(a,b:integer):integer;
  14.  
  15.   returns the maximum integer from the paramaters
  16.  
  17. ───────────────────────────────────────────────────────────────────────────
  18. function min(a,b:integer):integer;
  19.  
  20.   returns the minimum integer from the paramaters
  21.  
  22. ───────────────────────────────────────────────────────────────────────────
  23. function strint(s:string):longint;
  24.  
  25.   converts a four character string to a longint value;
  26.  
  27.   The characters are repesented at hi and lo byte values of the longint.
  28.  
  29.   EXAMPLE:
  30.  
  31.     var
  32.       s : string;
  33.       l : longint;
  34.     begin
  35.       s := 'AT';
  36.       l := strint(s); { l := 21569  or $00005441 }
  37.     end;
  38.  
  39. ───────────────────────────────────────────────────────────────────────────
  40. function intstr(l:longint):string;
  41.  
  42.    converts a longint to a four character string.
  43.  
  44.    EXAMPLE:
  45.  
  46.      var
  47.         s : string;
  48.         l : longint;
  49.      begin
  50.        s := intstr(21569);  { s := 'AT'#0#0; }
  51.      end;
  52.  
  53. ───────────────────────────────────────────────────────────────────────────
  54. function ups(s:string):string;
  55.  
  56.    returns S in all uppercase letters.
  57.  
  58. ───────────────────────────────────────────────────────────────────────────
  59. function st(h:longint):string;
  60.  
  61.    converts H to a string
  62.  
  63.    EXAMPLE:
  64.  
  65.       var
  66.          s : string;
  67.       begin
  68.          s := st(123);   { s := '123';
  69.       end;
  70.  
  71. ───────────────────────────────────────────────────────────────────────────
  72. function compare(s1,s2:string):boolean;
  73.  
  74.    Compares two strings returns true if they are equal up to the length
  75.    of S1.
  76.  
  77.    EXAMPLE:
  78.  
  79.        compare('SPXLIB','SPX')  returns FALSE
  80.        compare('SPX','SPXLIB')  returns TRUE
  81.  
  82. ───────────────────────────────────────────────────────────────────────────
  83. function dtcmp(var s1,s2;size:word):boolean;
  84.  
  85.    Same as function CMP.   Here for compatibility
  86.  
  87. ───────────────────────────────────────────────────────────────────────────
  88. function cmp(var s1,s2;size:word):boolean;
  89.  
  90.    Compares to variables of unknown types.
  91.  
  92.    S1,S2:   objects of any type to compare
  93.    SIZE:    size of objects
  94.  
  95.    Returns true if the bytes within S1 and S2 are equal
  96.  
  97. ───────────────────────────────────────────────────────────────────────────
  98. function lz(i,w:longint):string;
  99.  
  100.    Converts a longint to a string with leading zeros.
  101.  
  102.    I:    Integer to convert
  103.    W:    Number of spaces to place zeros
  104.  
  105.    EXAMPLE:
  106.  
  107.       var
  108.         s : string;
  109.       begin
  110.         s := lz(1234,7);  { s := '0001234'; }
  111.       end;
  112.  
  113. ───────────────────────────────────────────────────────────────────────────
  114. function vl(h:string):longint;
  115.  
  116.    converts a string to a longint value
  117.  
  118.    H:  string to convert
  119.  
  120. ───────────────────────────────────────────────────────────────────────────
  121. function spaces(h:integer):string;
  122.  
  123.    Creates a string with H number of spaces
  124.  
  125.    EXAMPLE:
  126.  
  127.        s := spaces(10); { s := '          '; (* ten spaces *) }
  128.  
  129. ───────────────────────────────────────────────────────────────────────────
  130. function repstr(h:integer;ch:char):string;
  131.  
  132.    Creates a string with H number of the character ch.
  133.  
  134.  
  135.    EXAMPLE:
  136.  
  137.       s := repstr(10,'A');  { s := 'AAAAAAAAAA'; }
  138.  
  139. ───────────────────────────────────────────────────────────────────────────
  140. function ifix(var a:integer;min,max:integer):boolean;
  141.  
  142.    Sets an integer to the range min..max.
  143.  
  144.    A:     Value to change
  145.    MIN:   Minimum value A can be
  146.    MAX:   Maximum value A can be
  147.  
  148.    Returns TRUE if the variable was changed.
  149.  
  150.    EXAMPLE:
  151.  
  152.      {$X+ }  { enable extended syntax }
  153.  
  154.      var
  155.        a,b,c     : integer;
  156.        modified,
  157.        didChange : boolean;
  158.      begin
  159.        a := -10;
  160.        b := 112;
  161.        c := 50;
  162.        didChange := ifix(a,0,100);
  163.        modified := ifix(c,0,100);
  164.        ifix(b,0,100);
  165.  
  166.          { didChange := TRUE }
  167.          { modified := FALSE }
  168.          { a := 0   }
  169.          { b := 100 }
  170.          { c := 50  }
  171.      end;
  172.  
  173. ───────────────────────────────────────────────────────────────────────────
  174. function rfix(var a:real;min,max:real):boolean;
  175.  
  176.   Sets a real to the range min..max.
  177.  
  178.    A:     Value to change
  179.    MIN:   Minimum value A can be
  180.    MAX:   Maximum value A can be
  181.  
  182.    Returns TRUE if the variable was changed.
  183.  
  184.    EXAMPLE
  185.        See function ifix
  186. ───────────────────────────────────────────────────────────────────────────
  187. function anything(s:string):boolean;
  188.  
  189.    Returns TRUE if the S is not an empty string or if any of
  190.      the characters in S is in the range #32..#255 (ascii)
  191.  
  192. ───────────────────────────────────────────────────────────────────────────
  193. function exist(f:string):boolean;
  194.  
  195.    Returns TRUE if the file exist on disk
  196.  
  197.    F:  The file to check.  Can have full file spec.
  198. ───────────────────────────────────────────────────────────────────────────
  199. function TPerror(errorcode:integer) : string;
  200.  
  201.    Returns an Turbo Pascal error string
  202.  
  203.    ERRORCODE:   value generated by the funciton IOresult
  204.  
  205. ───────────────────────────────────────────────────────────────────────────
  206. procedure funpad(var s:string);
  207.  
  208.    Removes all leading spaces from the string S
  209.  
  210. ───────────────────────────────────────────────────────────────────────────
  211. procedure unpad(var s:string);
  212.  
  213.    Removes all trailing spaces from the string S
  214.  
  215. ───────────────────────────────────────────────────────────────────────────
  216. procedure munpad(var s:string;b:byte);
  217.  
  218.    Cuts string S to length B.  Then removes all trailing charaters
  219.     that are in the range #0..#31.
  220.  
  221. ───────────────────────────────────────────────────────────────────────────
  222. function fpad(s:string;h:integer):string;
  223.  
  224.    Returns the string S with length H by adding trailing spaces
  225.    if nessasary.
  226.  
  227. ───────────────────────────────────────────────────────────────────────────
  228. procedure pad(var s:string;h:integer);
  229.  
  230.   Same as fpad.  Changes string S to length H by adding trailing spaces
  231.   if nessasary.
  232.  
  233. ───────────────────────────────────────────────────────────────────────────
  234. procedure fix(var s:string;h:string);
  235.  
  236.   Adds an extension to the file name S if one does not exist.
  237.  
  238.   S:  String to check for extenstion
  239.   H:  extension to add
  240.  
  241.   EXAMPLE:
  242.  
  243.     s1 := 'mypic';
  244.     s2 := 'mypic2.';
  245.     s3 := 'mypic3.dog';
  246.     s4 := 'mypic4.pcx';
  247.  
  248.     fix(s1,'.PCX');  { s1 := 'mypic.PCX'  }
  249.     fix(s2,'.PCX');  { s2 := 'mypic2.'    }
  250.     fix(s3,'.PCX');  { s3 := 'mypic3.dog' }
  251.     fix(s4,'.PCX');  { s4 := 'mypic.pcx'  }
  252.  
  253. ───────────────────────────────────────────────────────────────────────────
  254. procedure fixh(var s:string);
  255.  
  256.   Converts all characters in S that are in the range #0..#31 to spaces
  257.  
  258. ───────────────────────────────────────────────────────────────────────────
  259. function range(x,y,x1,y1,x2,y2:integer) : boolean;
  260.  
  261.   Returns TRUE if the point (x,y) is in the rectangle (x1,y1,x2,y2)
  262.  
  263. ───────────────────────────────────────────────────────────────────────────
  264. function rrange(x,y,x1,y1,x2,y2:real) : boolean;
  265.  
  266.   Returns TRUE if the point (x,y) is in the rectangle (x1,y1,x2,y2)
  267.  
  268. ───────────────────────────────────────────────────────────────────────────
  269. function between(x,x1,x2:longint):boolean;
  270.  
  271.   Returns TRUE if X is between X1 and X2
  272.  
  273. ───────────────────────────────────────────────────────────────────────────
  274. function fspaces(s:string;skip:byte):string;
  275.  
  276.   Returns a string portion in S delimited by spaces.  Returns an empty
  277.   string if one is not found.
  278.  
  279.   S:     string to search
  280.   SKIP:  number of strings to skip
  281.  
  282.   EXAMPLE:
  283.  
  284.      s := 'Now is the time for all good men to';
  285.  
  286.      writeln(fspaces(s,0));   { outputs 'Now'   }
  287.      writeln(fspaces(s,3));   { outputs 'time'  }
  288.      writeln(fspaces(s,9));   { outputs ''      }
  289.  
  290. ───────────────────────────────────────────────────────────────────────────
  291. function GetPtr(p:pointer;offset:longint):pointer;
  292.  
  293.    Creates a new pointer at P+offset.  Adjusts for segment boundries.
  294.    Returns the new pointer location.  Note: offset MUST be positive or zero.
  295.  
  296.    P:        pointer to adjust
  297.    OFFSET:   offset for the pointer (in bytes)
  298.  
  299.    EXAMPLE:
  300.  
  301.    var
  302.      p : pointer;
  303.  
  304.    p := GetPtr(SomePointer,88000);
  305.  
  306.    { p points to 88000 bytes past SomePointer }
  307.  
  308. ───────────────────────────────────────────────────────────────────────────
  309. function sar(value:integer;shift:byte):integer;
  310.  
  311.   Shift arithmetic right.  Same as SHR but maintains the signed (hi) bit.
  312.  
  313.   VALUE: integer value to shift.
  314.   SHIFT: bits to shift
  315.  
  316.   note: I don't know why Borland never added this operator! I hate
  317.      to type the typecast each time. ( integer(value shr shift) )
  318.  
  319.   EXAMPLE:
  320.  
  321.      writeln(sar(10,1));   { outputs   5 }
  322.      writeln(sar(-10,1));  { outputs  -5 }
  323.  
  324.  
  325. ───────────────────────────────────────────────────────────────────────────
  326. function sal(value:integer;shift:byte):integer;
  327.  
  328.   Shift arithmetic left.  Same as SHL but maintains the signed (hi) bit.
  329.  
  330.   VALUE: integer value to shift.
  331.   SHIFT: bits to shift
  332.  
  333.    EXAMPLE:
  334.  
  335.      writeln(sal(10,1));   { outputs   20 }
  336.      writeln(sal(-10,1));  { outputs  -20 }
  337.  
  338. ───────────────────────────────────────────────────────────────────────────
  339. function ptr2hex(p:pointer):string;
  340.  
  341.   Converts a pointer to a string in hexadecimal format
  342.  
  343.   P: pointer to convert
  344.  
  345.   p := ptr($a000,20);
  346.   writeln(ptr2hex(p));   { outputs   A000:0014 }
  347.  
  348. ───────────────────────────────────────────────────────────────────────────
  349. function word2hex(w:word):string;
  350.  
  351.   Converts a word value to hexadecimal format
  352.  
  353. ───────────────────────────────────────────────────────────────────────────
  354. function byte2hex(b:byte):string;
  355.  
  356.   Converts a byte value to hexadecimal format
  357.  
  358. ───────────────────────────────────────────────────────────────────────────
  359. procedure atexit(proc:QuitProc);
  360.  
  361.   Same as the function in C.  Calls a quit procedure at exit.  Can use
  362.    many times. Chains multiple procedures.  At the exit of the program
  363.    the procedure are called in a LIFO fashion.  (Last in-First out) The
  364.    last atexit procedure is the first one to be called.
  365.  
  366.   PROC:  procedure to call at the end of the program.  Must be declared
  367.     as a far procedure.
  368.  
  369.   EXAMPLE:
  370.  
  371.      Uses spx_fnc;
  372.  
  373.      {$F+ }
  374.      procedure cleanup;
  375.      begin
  376.        { do my clean up routines here }
  377.      end;
  378.  
  379.      procedure returnToTextMode;
  380.      begin
  381.        textmode(c80);
  382.      end;
  383.  
  384.      begin
  385.        atexit(cleanup);
  386.        atexit(returnToTextMode);
  387.      end.